import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { DeleteResult, EntityManager, Repository } from 'typeorm';
import { Role } from './role.entity';
import { CreateRoleDto } from './role.dto';

@Injectable()
export class RoleService {
    constructor (
      @InjectRepository(Role)
      private readonly roleRepository: Repository<Role>
    ) { }

    // 创建数据,传递一个对象类型的数据
    async create(data: CreateRoleDto, manager: EntityManager,) {
      const { name, description} = data;
      const role: { [propName: string]: any } = await manager.save(
        Role,
        {
          name,
          description,
        },
      );
      return '创建成功';
    }

    // 查询全部的数据
    async list(): Promise<Role[]> {
        return await this.roleRepository.find();
    }

    // 删除数据
    async delete(id): Promise<DeleteResult> {
        return await this.roleRepository.delete(id);
    }

}
